//
//  HMVisitorView.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/27.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SnapKit
//协议方法
//protocol HMVisitorViewDelegate:NSObjectProtocol{
//    func loginButtonClick()
//    func registerButtonClick()
//}


class HMVisitorView: UIView{
    //设置代理
   // weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 执行逻辑
    //提供外部调用
    func setVisitorInfo(imgName: String? ,title: String?){
        if let img = imgName , tit = title {
            iconView.image = UIImage(named: img)
            messageLabel.text = tit
            circleView.hidden = true
        }else{
            startAnim()
        }
    }
    
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //旋转360度
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        //执行时间
        anim.duration = 20
        
        //在切换tabbar 或者将应用切换到后台 anim 默认会被释放掉
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
//    @objc private func loginButtonClick(){
//        delegate?.loginButtonClick()
//    }
//    
//    @objc private func registerButtonClick(){
//        delegate?.registerButtonClick()
//    }
//    
    // MARK: - 懒加载控件
    /// iconView
    private lazy var iconView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    
    private lazy var circleView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imageView
    }()
    
    private lazy var maskImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(14)
        lab.textColor = UIColor.darkGrayColor()
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        return lab
    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
     //   button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
     //   button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
}

extension HMVisitorView{
    // MARK: - 添加控件
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 1. 添加控件
        addSubview(circleView)
        addSubview(maskImageView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2. 添加约束
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        
        
        
    }
    
    private func demo1(){
        // 2. 添加约束
        // 2.1 iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 2.2 circleView
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 2.3 messageLabel
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 约束宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 2.4 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 2.5 登陆按钮
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        // 2.2 maskView
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    }
}
